//
//  ScanViewViewController.m
//  YYQRScaning
//
//  Created by 蓓蕾 on 2021/8/12.
//  Copyright © 2021 Yang. All rights reserved.
//

#import "ScanViewViewController.h"

#import "YYQRCodeView.h"

@interface ScanViewViewController ()
@property (nonatomic, strong) YYQRCodeView *codeView;
@end

@implementation ScanViewViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    _codeView = [[YYQRCodeView alloc] initWithFrame:self.view.bounds];
    [self.view addSubview:_codeView];
    //扫码成功的回调
    __weak typeof(self) weakSelf = self;
    _codeView.backQRCodeURL = ^(NSString *stringValue) {
        [weakSelf showAlertWithTitle:@"扫描结果" withInfo:stringValue withCancelTitle:nil withConfirmTitle:@"确定" withResultBlock:^{
            [weakSelf.codeView starScaning];
        }];
    };
    //扫码失败的回调
    _codeView.backfailure = ^{
        
    };
}

-(void)showAlertWithTitle:(NSString *)title withInfo:(nullable NSString *)info withCancelTitle:(nullable NSString *)cancelString withConfirmTitle:(NSString *)confirmString withResultBlock:(void(^)(void))result
{
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:info preferredStyle:UIAlertControllerStyleAlert];
    
    if (cancelString.length > 0) {
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:cancelString style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        [alertController addAction:cancelAction];
    }
    
    UIAlertAction *confirmAction = [UIAlertAction actionWithTitle:confirmString style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        if (result) {
            result();
        }
    }];
    [alertController addAction:confirmAction];
    [self presentViewController:alertController animated:YES completion:nil];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
